package com.company.action;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.management.Query;

import org.apache.struts2.ServletActionContext;

import com.company.dao.AccountsDao;
import com.company.dao.CompanyinfoDao;
import com.company.entity.Accounts;
import com.company.entity.CompanyInfo;
import com.company.service.AccountsService;
import com.company.service.CompanyinfoService;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.opensymphony.xwork2.ActionSupport;

public class CompanyinfoAction extends BaseAction {

	private CompanyInfo companyInfo;
	private CompanyinfoService companyinfoService;

	public CompanyInfo getCompanyInfo() {
		return companyInfo;
	}

	public void setCompanyInfo(CompanyInfo companyInfo) {
		this.companyInfo = companyInfo;
	}

	public CompanyinfoService getCompanyinfoService() {
		return companyinfoService;
	}

	public void setCompanyinfoService(CompanyinfoService companyinfoService) {
		this.companyinfoService = companyinfoService;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		Pattern pattern = Pattern.compile("(\r\n|\r|\n|\n\r)");
		// 正则表达式的匹配一定要是这样，单个替换\r|\n的时候会错误
		Matcher matcher = pattern.matcher(companyinfoService.query());
		String newString = matcher.replaceAll("<br>");
		ServletActionContext.getRequest().getSession()
				.setAttribute("company_info", newString);
		return super.execute();
	}

	public String list() {
		setCompanyInfo(companyinfoService.list());
		if (companyInfo != null) {
			ServletActionContext.getRequest().getSession()
					.setAttribute("company_info", companyInfo);
			return SUCCESS;
		}
		return ERROR;
	}

	public String update() throws Exception {
		if (companyInfo == null) {
			return ERROR;
		}
		companyinfoService.update(companyInfo);
		this.list();
		return SUCCESS;
	}
}
